﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using Newtonsoft.Json;
using Sovigaz.DataProvider;
using Sovigaz.Permission.Repositories;
using Utilities;

namespace Sovigaz.Permission.Api
{
    public class LotApiController : ApiController
    {
        public LotRepository Get(int? pageNumber, string orderBy, string orderDirection, string lotCode)
        {
            try
            {
                string whereClause = "IsDeleted != true";
                if (!String.IsNullOrWhiteSpace(lotCode))
                    whereClause = String.Format(whereClause + " AND lotCode.Contains(\"{0}\")", lotCode);

                return new LotRepository().GetList(whereClause, orderBy, orderDirection, pageNumber ?? 1);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public Lot Get(int? id)
        {
            try
            {
                Lot lot = new Lot();
                var lotRepository = new LotRepository();
                lot.LotCode = GenerateCode.GetCode(lotRepository.GetNextIndex(), CodeType.Lot);
                if (id.HasValue && id > 0)
                lot = lotRepository.GetByID(id.Value);
                return lot;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public void Delete(int? id)
        {
            try
            {
                if (id.HasValue && id > 0)
                {
                    var lotRepository = new LotRepository();
                    lotRepository.Delete(id.Value);
                }
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public LotRepository Delete(string ids, int? pageNumber, string orderBy, string orderDirection, string lotCode)
        {
            try
            {
                if (ids.StartsWith(","))
                    ids = ids.Remove(0, 1);
                var idArr = ids.Split(',');

                var lotRepository = new LotRepository();
                lotRepository.Delete(idArr);

                return Get(pageNumber, orderBy, orderDirection, lotCode);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public Lot Put(Lot lot)
        {
            try
            {
                var lotRepository = new LotRepository();
                int lotID = lotRepository.Update(lot);

                return Get(lotID);
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException(e.Message, e);
            }
        }

        public List<Lot> Get()
        {
            try
            {
                return new LotRepository().GetListLot();
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }   
    }
}
